package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.service.ISysStudentService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTrain;
import com.ruoyi.system.service.ISysTrainService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实训记录Controller
 * 
 * @author ruoyi
 * @date 2024-12-27
 */
@Controller
@RequestMapping("/system/train")
public class SysTrainController extends BaseController
{
    private String prefix = "system/train";

    @Autowired
    private ISysTrainService sysTrainService;

    @Autowired
    private ISysStudentService sysStudentService;

    @RequiresPermissions("system:train:view")
    @GetMapping()
    public String train()
    {
        return prefix + "/train";
    }

    /**
     * 查询实训记录列表
     */
    @RequiresPermissions("system:train:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTrain sysTrain)
    {
        startPage();
        List<SysTrain> list = sysTrainService.selectSysTrainList(sysTrain);
        return getDataTable(list);
    }

    /**
     * 导出实训记录列表
     */
    @RequiresPermissions("system:train:export")
    @Log(title = "实训记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTrain sysTrain)
    {
        List<SysTrain> list = sysTrainService.selectSysTrainList(sysTrain);
        ExcelUtil<SysTrain> util = new ExcelUtil<SysTrain>(SysTrain.class);
        return util.exportExcel(list, "实训记录数据");
    }

    /**
     * 新增实训记录
     */
    @GetMapping("/add/{studentId}")
    public String add(@PathVariable("studentId") Long studentId, ModelMap mmap)
    {
        SysStudent sysStudent = sysStudentService.selectSysStudentByStudentId(studentId);
        mmap.put("sysStudent", sysStudent);
        return prefix + "/add";
    }
    /**
     * 新增保存实训记录
     */
    @RequiresPermissions(value = {"system:train:add", "system:student:add"}, logical = Logical.OR)
    @Log(title = "实训记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTrain sysTrain)
    {
        return toAjax(sysTrainService.insertSysTrain(sysTrain));
    }

    /**
     * 修改实训记录
     */
    @RequiresPermissions(value = {"system:train:edit", "system:student:edit"}, logical = Logical.OR)
    @GetMapping("/edit/{trainId}")
    public String edit(@PathVariable("trainId") Long trainId, ModelMap mmap)
    {
        SysTrain sysTrain = sysTrainService.selectSysTrainByTrainId(trainId);
        mmap.put("sysTrain", sysTrain);
        return prefix + "/edit";
    }

    /**
     * 修改保存实训记录
     */
    @RequiresPermissions(value = {"system:train:edit", "system:student:edit"}, logical = Logical.OR)
    @Log(title = "实训记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTrain sysTrain)
    {
        return toAjax(sysTrainService.updateSysTrain(sysTrain));
    }

    /**
     * 删除实训记录
     */
    @RequiresPermissions(value = {"system:train:remove", "system:student:remove"}, logical = Logical.OR)
    @Log(title = "实训记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTrainService.deleteSysTrainByTrainIds(ids));
    }
}
